﻿using System;
using System.Collections.Generic;
using System.Text;
using RestSharp;
using EasyClassUI.entity;
using Newtonsoft.Json;

namespace EasyClassUI.WebRequest {

    // 创建群组时的返回值，包含groupID以及StatusCode
    public class CreateGroupStatus {
        public string groupID { get; set; }
        public string Status { get; set; }
    }

    // 查询成员时的返回值，包含成员以及StatusCode
    public class QueryMemberStatus {
        public string[] Users { get; set; }
        public string Status { get; set; }
    }

    // 查询群组时的返回值，包含群组以及StatusCode
    public class QueryGroupStatus {
        public StudyGroup[] Groups { get; set; }
        public string Status { get; set; }
    }

    // 查询群组名称时的返回值，包含群组名以及StatusCode
    public class QueryGroupNameStatus {
        public string[] Groups { get; set; }
        public string Status { get; set; }
    }

    /*---------------------------------群组服务类--------------------------------- */
    class GroupService {
        private string groupURL = "https://localhost:5001/group/";

        public GroupService() {

        }

        // 创建群组
        public CreateGroupStatus CreateGroup(string Token, StudyGroup newGroup) {
            var client = new RestClient(groupURL + "add");
            string json = newGroup.ExportCreateJson();
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", Token);
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("application/json", json, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            string ID = response.Content.ToString();
            ID = ID.Replace("\"", "");
            return new CreateGroupStatus() { Status = status, groupID = ID };
        }

        // 加入群组
        public string JoinGroup(string groupID, string Token) {
            var client = new RestClient($"{groupURL}join?StudyGroupID={groupID}");
            client.Timeout = -1;
            var request = new RestRequest(Method.PUT);
            request.AddHeader("Authorization", Token);
            request.AlwaysMultipartFormData = true;
            IRestResponse response = client.Execute(request);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            switch (status) {
                case "200": return "200";
                case "400": return "400";
                default: return "404";
            }
        }

        // 上传文件到群组
        public string UploadFile(List<string> filesToUpload, string groupID) {
            var client = new RestClient($"https://localhost:5001/resource/upload?isGroup=true&ID={groupID}");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            foreach (string file in filesToUpload) {
                request.AddFile("body", file);
            }
            IRestResponse response = client.Execute(request);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            switch (status) {
                case "200": return "200";
                case "400": return "400";
                default: return "404";
            }
        }

        // 按名字查找群组
        public QueryGroupStatus SearchGroupByName(string keyWord) {
            var client = new RestClient($"{groupURL}Searchbyname?SearchName={keyWord}");
            client.Timeout = -1;
            var request = new RestRequest(Method.GET);
            IRestResponse response = client.Execute(request);
            string searchResult = response.Content.ToString();
            StudyGroup[] result = JsonConvert.DeserializeObject<StudyGroup[]>(searchResult);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            return new QueryGroupStatus() { Groups = result, Status = status };
        }

        // 查询一个群组内的所有成员
        public QueryMemberStatus QueryAllMember(string groupID) {
            var client = new RestClient($"{groupURL}queryaccount?StudyGroupID={groupID}");
            client.Timeout = -1;
            var request = new RestRequest(Method.GET);
            IRestResponse response = client.Execute(request);
            string searchResult = response.Content.ToString();
            string[] result = JsonConvert.DeserializeObject<string[]>(searchResult);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            return new QueryMemberStatus() { Users = result, Status = status };
        }

        // 查询一个人加入了哪些群组
        public QueryGroupStatus QueryWhichGroupHasBeenJoin(string Token) {
            var client = new RestClient($"{groupURL}querygroups");
            client.Timeout = -1;
            var request = new RestRequest(Method.GET);
            request.AddHeader("Authorization", Token);
            IRestResponse response = client.Execute(request);
            string searchResult = response.Content.ToString();
            StudyGroup[] result = JsonConvert.DeserializeObject<StudyGroup[]>(searchResult);
            string status = Convert.ToInt32(response.StatusCode).ToString();
            return new QueryGroupStatus() { Groups = result, Status = status };
        }
    }
}
